var express = require('express');
var conn = require('../public/javascripts/connect');
var router = express.Router();
router.get( "/",function (req, res){
    res.render('changepassword',{})
})
router.post( "/",async function (req, res){
    let {Phone,oldpasword,newpaw,repeatpas}=req.body;
    console.log(Phone);
    console.log(oldpasword);
    console.log(newpaw);
    console.log(repeatpas);
    let sqlSel=`SELECT * FROM user WHERE  user_pwd='${oldpasword}' AND phone='${Phone}'`;
    console.log("sqlSel",sqlSel);
    let sql=`UPDATE user SET user_pwd='${newpaw}' WHERE phone='${Phone}'`;
    await new Promise(resSel=>{
        conn.connection1.query(sqlSel,function (err,result,files){
            if(err){
                console.log("ERROR",err);
                return;
            }
            console.log("result",result);
            if(result==false){//失败
                resSel(false);
            }else{
                resSel(true)
            }
        });
    }).then(data=>{
        if(data){
            conn.connection1.query(sql,function (err,result,files) {
                if(err){
                    console.log("ERROR",err);
                    return;
                }
                res.render("login");
            });
        }else{
            res.render("changepassword",{data:"用户名或密码错误,请重新输入"})
        }
    })

})
module.exports = router;
